home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#26 (Nov 87)
/
asm lab Formatter
/
NumToString.asm
< prev
next >
Wrap
Assembly Source File
|
1987-10-21
|
1KB
|
59 lines
; NumToString.asm
;-----------------
; by Mike™ Scanlin 28 Dec 1986
; an alternative to _NumToString
Xref NumToString
;==========
NumToString
;==========
; convert a 32 bit integer into a pascal string
; input: D0 longint
; A0 points to a space of at least 12 bytes
; output: A0 points to pascal string
MOVEM.L D0-D4/A1-A2,-(SP)
LEA 1(A0),A1 ;skip length byte
TST.L D0 ;is number zero?
BGT.S @2
BMI.S @1
MOVE.B #'0',(A1)+ ;special case for zero
BRA.S @8
@1 MOVE.B #'-',(A1)+ ;give string a minus sign
NEG.L D0 ;make number positive
@2 LEA PowersTable,A2
MOVEQ #1,D3 ;set leading zeros flag
MOVEQ #9,D4 ;loop counter
@3 MOVE.L (A2)+,D2 ;get a power of 10
MOVEQ #'0',D1 ;init digit
@4 CMP.L D2,D0 ;is # > power of 10?
BLT.S @5
ADDQ #1,D1 ;increase digit
SUB.L D2,D0 ;subtract power of 10
BNE.S @4
@5 TST D3 ;have we had a non-zero digit yet?
BEQ.S @6
CMP.B #'0',D1 ;is this a leading zero?
BEQ.S @7
MOVEQ #0,D3 ;print all zeros from now on
@6 MOVE.B D1,(A1)+
@7 DBRA D4,@3
@8 MOVE A1,D0 ;calc length of new string
SUB A0,D0
SUBQ.B #1,D0 ;minus 1 for length byte
MOVE.B D0,(A0)
MOVEM.L (SP)+,A1-A2/D0-D4
RTS
PowersTable DC.L 1000000000
DC.L 100000000
DC.L 10000000
DC.L 1000000
DC.L 100000
DC.L 10000
DC.L 1000
DC.L 100
DC.L 10
DC.L 1